package hy.planegame.sprite

import android.graphics.Canvas
import android.graphics.RectF
import hy.planegame.utils.paint
import hy.planegame.utils.playerBoom

/**
 * Created time : 2017/8/31 18:05.
 * @author HY
 */
class PlayerBoom : Sprite<PlayerBoom>() {
    private val width = playerBoom.width * 2 / 3f
    private val height = playerBoom.height * 2 / 3f

    private val singleWidth = width / 7f

    private var pos = 0
    private var count = 1 //延迟计数器
    private var isDraw = false

    private var centerX = 0  //爆炸中心x坐标
    private var centerY = 0  //爆炸中心y坐标
    private val rectF = RectF()

    fun start(centerX: Int, centerY: Int) {
        this.centerX = centerX
        this.centerY = centerY
        isDraw = true
    }

    override fun draw(canvas: Canvas?) {
        canvas ?: return
        if (isDraw) {
            val x = centerX - singleWidth / 2 - singleWidth * pos
            rectF.set(x,
                    centerY - height / 2,
                    x + width,
                    centerY + height / 2)

            canvas.save()
            canvas.clipRect(centerX - singleWidth / 2, centerY - height / 2, centerX + singleWidth / 2, centerY + height / 2)
            canvas.drawBitmap(playerBoom, null, rectF, paint)
            canvas.restore()
        }
    }

    override fun logic() {
        if (isDraw) {
            if (count % 2 == 0) {
                if (pos < 7) {
                    pos++
                } else {
                    _onStop?.invoke(this)
                }
            }
            count++
        }
    }

    override fun destroy() {
    }
}